import Vue 			from 'vue'
import util 		from '../../util'
import axios 		from 'axios'
import { Toast,Dialog,List } 	from 'vant';
Vue.use(Toast,Dialog,List);
import {BigNumber}  from 'bignumber.js'

export default {
	name: 'redExchange',
	data(){
		return {
			totalBtc:0,				//持有的BTC数量
			list:[],				//可兑换券列表
			dialogEx:{}
		}
	},
	methods:{
		goExchange(ele){
			//点击兑换
			if(ele.value === 0) return
			// console.log(ele)
			Dialog.confirm({
				title: '兑换BTC券',
				message: '你将使用'+ (ele.value*ele.exchange_rate).toFixed(8) +'BTC<br />兑换'+ ele.value +'张'+ ele.coupon_amount +'BTC券，是否继续？',
				className:'redExchange-dialog',
				cancelButtonColor:'#1989fa',
				confirmButtonText:'确认',
				cancelButtonText:'取消',
			}).then(() => {
				Toast.loading({
					message: '请稍后...',
					duration:0,
					forbidClick: true
				});
				util.fetch2(`${util.ajaxUrl}/red-packet/exchange`,{
					token:this.token || util.getToken(),
					coupon_amount:ele.coupon_amount,
					coupon_nums:ele.value
				}).then(res => {
					Toast(res.retMsg || res.retHtml || '未知错误')
					this.getTotalBTC();
				})
				console.log('confirm')
			}).catch(() => {
				console.log('cancel')
			})
		},
		goReduce(ele){
			//减少数量
			this.list.map(item => {
				if(item.id == ele.id) item.value = item.value == 0?0:item.value-1;
			})
		},
		goAdd(ele){
			//增加数量
			this.list.map(item => {
				if(item.id == ele.id) item.value = item.value+1>item.max?item.max:item.value+1;
			})
		},
		redPacketExchangeConfRequest(){
			//兑换券配置
			util.fetch2(`${util.ajaxUrl}/site/exchange-config`,{}).then(res => {
				if(res.retCode === 0){
					res.retData.list.map((item,index) => {
						item.id = index;
						item.value = 0;
						item.min = 0;
						let x = new BigNumber(this.totalBtc);
						let y = new BigNumber(item.exchange_rate);
						item.max = Math.floor(Number(x.dividedBy(y).toString()));
					});
					this.list = res.retData.list;
					Toast.clear();
				}else{
					Toast(res.retMsg || res.retHtml || '未知错误')
				}
			})
		},
		getTotalBTC(){
			//获取持有的BTC数量
			util.fetch2(`${util.ajaxUrl}/red-packet/total`,{
				token:this.token || util.getToken(),
			}).then(res => {
				if(res.retCode === 0){
					this.totalBtc = util.formatBTC(res.retData.total);
					// this.totalBtc = 0.0006;
				}else{
					Toast(res.retMsg || res.retHtml || '未知错误')
				}
				this.redPacketExchangeConfRequest();
			})
		},
		init(){
			Toast.loading({
				message: '加载中...',
				duration:0,
				forbidClick: true
			});
			this.getTotalBTC();
		},
		goBack(){
			util.navigateBack(-1,this)
		}
	},
	created() {
		util.waitSDKReady().then(token => {
			this.token = token;
			this.init();
		}).catch((e) => {
			Toast('获取token失败')
		})
		setTimeout(() => {
			util.registerHandler('refreshCommonRequestWhenGoBackView',res => {
				if(this.token) this.init();
				util.callHandler('setStatusBarStyle',{
					style:'dark'
				})
			})
			//退出登录的时候native调用H5方法清除token缓存
			util.registerHandler('loginOutDeleteToken',res => {
				window.token = '';
				this.token = '';
				localStorage.removeItem('token');
			})
			util.callHandler('setStatusBarStyle',{
				style:'dark'
			})
		},500)
    }
}